package it.getta.vicinometro.popup;

import it.getta.vicinometro.ActivityCallBack;
import it.getta.vicinometro.R;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WarningPopup extends VicinometroPopup {
	
	public static final int BKG_SEMI_TRASPARENT = 0;
	public static final int BKG_NOT_TRASPARENT = 1;
	
	private OnPopupClickListener mOnPositiveButtonListener;
	private OnPopupClickListener mOnNegativeButtonListener;
	private boolean mSingleButton;
	private TextView mTitle;
	private TextView mMessage;
	private String mPositiveButtonText;
	private String mNegativeButtonText;
	private int mBackgroundStyle = -1;

	public WarningPopup(Context context, ActivityCallBack activityCallBack) {
		super(context, activityCallBack, R.layout.warning_popup);
		mSingleButton = false;
	}
	
	public WarningPopup(Context context, ActivityCallBack activityCallBack, boolean singleButton) {
		super(context, activityCallBack, R.layout.warning_popup);
		mSingleButton = singleButton;
	}
	
	public void setPositiveButtonText(String text) {
		mPositiveButtonText = text;
	}
	
	public void setNegativeButtonText(String text) {
		mNegativeButtonText = text;
	}
	
	public void setSingleButton(boolean singleButton) {
		mSingleButton = singleButton;
	}
	
	public boolean isSingleButton() {
		return mSingleButton;
	}
	
	public void setTitle(String title) {
		if(mTitle == null) {
			mTitle = (TextView) mPopupRootView.findViewById(R.id.warning_popup_title);
		}
		mTitle.setText(title);
	}
	
	public void setTitle(int titleResId) {
		setTitle(mContext.getResources().getString(titleResId));
	}
	
	public void setMessage(String message) {
		if(mMessage == null) {
			mMessage = (TextView) mPopupRootView.findViewById(R.id.warning_popup_text);
		}
		mMessage.setText(message);
	}
	
	public void setMessage(int messageResId) {
		setMessage(mContext.getResources().getString(messageResId));
	}

	@Override
	public void showPopup() {
		super.showPopup();
		LinearLayout background = (LinearLayout)mPopupRootView.findViewById(R.id.warning_popup_background_linear);
		if(mBackgroundStyle == BKG_NOT_TRASPARENT) {
			background.setBackgroundResource(R.drawable.bkg_popup_opaque);
		} else if (mBackgroundStyle == BKG_SEMI_TRASPARENT) {
			background.setBackgroundResource(R.drawable.bkg_popup);
		}
		Button ok = (Button)mPopupRootView.findViewById(R.id.warning_popup_ok);
		if(mPositiveButtonText != null && mPositiveButtonText.trim().length()>0) {
			ok.setText(mPositiveButtonText);
		}
		ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mOnPositiveButtonListener != null) {
					mOnPositiveButtonListener.onClick();
				}
			}
		});
		Button cancel = (Button) mPopupRootView
				.findViewById(R.id.warning_popup_cancel);
		if (!mSingleButton) {
			if(mNegativeButtonText != null && mNegativeButtonText.trim().length()>0) {
				cancel.setText(mNegativeButtonText);
			}
			cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mOnNegativeButtonListener != null) {
						mOnNegativeButtonListener.onClick();
					}
				}
			});
		} else {
			cancel.setVisibility(View.GONE);
		}
		show(mActivityCallBack.getPopupAnchor());
	}
	
	public interface OnPopupClickListener {
		public void onClick();
	}
	
	public void setOnPositiveButtonListener(OnPopupClickListener listener) {
		mOnPositiveButtonListener = listener;
	}
	
	public void setOnNegativeButtonListener(OnPopupClickListener listener) {
		mOnNegativeButtonListener = listener;
	}

	public int getBackgroundStyle() {
		return mBackgroundStyle;
	}

	public void setBackgroundStyle(int backgroundStyle) {
		if (backgroundStyle == BKG_NOT_TRASPARENT || backgroundStyle == BKG_SEMI_TRASPARENT) {
			this.mBackgroundStyle = backgroundStyle;
		} else {
			this.mBackgroundStyle = BKG_SEMI_TRASPARENT;
		}
	}

}
